﻿#include "PostloopBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

PostloopBlock::PostloopBlock(QString &name, QRect &position, double zoom, bool isExpand, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
    m_isExpand = isExpand;
}

PostloopBlock::~PostloopBlock()
{

}

void PostloopBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = static_cast<double>(m_aScale)* zoom();
    double b = static_cast<double>(m_bScale) * a;
    double bottom = y + height;
    double lineScale=static_cast<double>(m_lineScale);
    //                /* Post cycle */
    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + lineScale * zoom()));
    if(m_isExpand)
    {
        QBlock *left = getBranchItem().at(0);
        Q_ASSERT_X(left != nullptr, "QBlock::paint()" ,"item(0) == 0. i.e. body of POST-loop is nul.");
        // 输入箭头菱形顶部
        double top = left->m_dY + left->m_dHeight + lineScale * zoom();

        canvas->drawLine(QLineF(hcenter,left->m_dY + left->m_dHeight, hcenter, top));

        drawBottomArrow(canvas, QPointF(hcenter, top),
                                    QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));
        QPointF par[4];
        par[0] = QPointF(hcenter - b/2, top + a/2);
        par[1] = QPointF(hcenter      , top      );
        par[2] = QPointF(hcenter + b/2, top + a/2);
        par[3] = QPointF(hcenter      , top + a  );

        QBrush brush = canvas->brush();
        canvas->setBrush(m_backgroundColor);
        canvas->drawPolygon(par, 4);
        canvas->setBrush(brush);
        UseOldPen
        QRectF rect(hcenter - b/2, top, b, a);

        canvas->drawText(rect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1").arg(name()));

        // 连接
        QPointF collector[4];
        collector[0] = QPointF(hcenter - b/2, top + a/2);
        collector[1] = QPointF(x + 5*zoom(), top + a/2);
        collector[2] = QPointF(x + 5*zoom(), y + 8*zoom());
        collector[3] = QPointF(hcenter, y + 8*zoom());

        SetNewPen(2)
        canvas->drawPolyline(collector, 4);
        drawRightArrow(canvas, collector[3],
                QSize(static_cast<int>(12 * zoom()), static_cast<int>(6 * zoom())));

        // 出口
        canvas->drawLine(QLineF(hcenter, top + a, hcenter, bottom+0.5));
        UseOldPen

        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter - b/2 - 24*zoom(), top - 4* zoom() + a/2), QObject::tr("Yes"));
        canvas->drawText(QPointF(hcenter  +4*zoom(), top + lineScale * zoom() + a), QObject::tr("No"));
    }
    else{
        drawBottomArrow(canvas,  QPointF(hcenter, y + m_lineScale * zoom()),
                        QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));

        QPointF par[3][4];
        for(int i = 0 ; i < 3; i++)
        {
            par[i][0] = QPointF(hcenter - b/2 + 3*i, y + m_lineScale * zoom() + a/2 + 3*i);
            par[i][1] = QPointF(hcenter + 3*i      , y + m_lineScale * zoom() + 3*i);
            par[i][2] = QPointF(hcenter + b/2 + 3*i, y + m_lineScale * zoom() + a/2 + 3*i);
            par[i][3] = QPointF(hcenter + 3*i      , y + m_lineScale * zoom() + a   + 3*i);
        }
        QColor bgColor = m_collapseBgColor;
        QBrush brush = canvas->brush();
        canvas->setBrush(bgColor);
        for(int di = 2; di >= 0; di--)
        {
            canvas->drawPolygon(par[di], 4);
        }
        canvas->setBrush(brush);
        UseOldPen
        QRectF textRect(hcenter - b/2, y + m_lineScale * zoom(), b , a);
        canvas->drawText(textRect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1").arg(name()));
        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter + b/3, y + 12 * zoom() + a/3), QObject::tr("Fold"));
    }

}

QString PostloopBlock::getBlockDom()
{
    QString xmlStr = QString("<algorithm><branch><post cond=\"x &lt; n\" id=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\" isPlugin=\"true\"><branch /></post></branch></algorithm>");
    return  xmlStr;
}
}
}
